package com.itcast;

import com.itcast.domain.Student;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ControllerSystem {
    private  static ArrayList<Student> al = new ArrayList<>();
    public static void main(String[] args) {
        al.add(new Student("111", "lwd", 12, "1213124"));
        al.add(new Student("112", "lwd", 13, "1213124"));
        al.add(new Student("113", "lwd", 14, "1213124"));
        al.add(new Student("114", "lwd", 15, "1213124"));
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("       *******       ");
            System.out.println("    *************   ");
            System.out.println(" *******************");
            System.out.println("*一点也不NB的学生管理系统");
            System.out.println("*-----1.添加            *");
            System.out.println("*-----2.查询            *");
            System.out.println("*-----3.删除            *");
            System.out.println("*-----4.修改            *");
            System.out.println("*-----5.退出            *");
            System.out.println("************************");
            String next = sc.next();
            int id = 0;
            switch (next) {
                case "1":
                    addStudent(id);
                    break;
                case "2":
                    showStudent();
                    break;
                case "3":
                    deleteStudent();
                    break;
                case "4":
                    modifyStudent();
                    break;
                case "5":
                    return;
                default:
                    System.out.println("输入错误");
            }
        }
    }
    public  static void addStudent(int id) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            //System.out.println("输入学号：");
            String nextId = String.format("heima%04d",id);
            if (getIndex(al, nextId) != -1) {
                System.out.println("输入的学号已经存在,请重新输入");
//
            }else{
                System.out.println("输入姓名：");
                String nextName = sc.next();
                System.out.println("输入年龄：");
                int nextAge = sc.nextInt();
                System.out.println("输入籍贯：");
                String nextAddress = sc.next();

                Student s = new Student(nextId, nextName, nextAge, nextAddress);
                al.add(s);
                System.out.println("\t\t\t添加成功");
                id++;
                break;
            }

        }

    }
    public  static void showStudent() {
        if (al.size() == 0) {
            System.out.println("没有数据请添加，选择1.进行添加");
            return;
        }
        for (int i = 0; i < al.size(); i++) {
            System.out.println(al.get(i));
        }
    }
    public static void deleteStudent() {
        System.out.println("请输入要删除学生的学号");
        Scanner sc = new Scanner(System.in);
        while (true) {
            int index = getIndex(al, sc.next());
            if (index == -1) {
                System.out.println("请输入正确的学号");
            } else {
                al.remove(index);
                System.out.println("删除成功");
                break;
            }
        }

    }
    public  static void modifyStudent() {
        System.out.println("请输入要修改学生的学号");
        Scanner sc = new Scanner(System.in);
        while (true) {
            int index = getIndex(al, sc.next());
            if (index == -1) {
                System.out.println("请输入正确的学号");
            } else {
                Student student = al.get(index);
                System.out.println("请输入新的名称");
                String name = sc.next();
                student.setName(name);
                System.out.println("请输入新的年龄");
                int age = sc.nextInt();
                student.setAge(age);
                System.out.println("请输入新的地址");
                String address = sc.next();
                student.setAddress(address);
                System.out.println("修改成功");
                break;
            }
        }

    }
    public static int getIndex(ArrayList<Student> list, String str){
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            if (str.equals(list.get(i).getId())) {
                index = i;
                break;
            }
        }
        return index;
    }
//    public static String[]  getRndomId(){
//        StringBuilder stringBuilder = new StringBuilder("heima");
//        Random r = new Random();
//        String [] strings = new String [3];
//        for (int i = 0; i < 3; i++) {
//            int id1 = r.nextInt(10);
//            int id2 = r.nextInt(10);
//            int id3 = r.nextInt(10);
//            String randomId = stringBuilder.append(id1).append(id2).append(id3).toString();
//            strings[i] = randomId;
//        }
//        return strings;
//    }

}
